package cn.itwen.star.graph.core.controller;


import cn.itwen.star.graph.core.dto.common.PageRequestDto;
import cn.itwen.star.graph.core.dto.common.PageResult;
import cn.itwen.star.graph.core.dto.common.Result;
import cn.itwen.star.graph.core.dto.request.Text2ImageReqDto;
import cn.itwen.star.graph.core.dto.respone.CanelTaskResDto;
import cn.itwen.star.graph.core.dto.respone.Text2ImageResDto;
import cn.itwen.star.graph.core.mapper.UserMapper;
import cn.itwen.star.graph.core.mapper.UserResultMapper;
import cn.itwen.star.graph.core.pojo.UserResult;
import cn.itwen.star.graph.core.service.Text2ImageService;
import cn.itwen.star.graph.core.utils.UserUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.coyote.BadRequestException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/authed/1.0/t2i")
public class TextToImageController {

    @Autowired
    private Text2ImageService text2ImageService;

    @Autowired
    private UserResultMapper userResultMapper;

    @PostMapping("/propmt")
    public Result<Text2ImageResDto> propmt(@RequestBody Text2ImageReqDto text2ImageReqDto) throws Exception {
        Text2ImageResDto text2ImageResDto = text2ImageService.propmt(text2ImageReqDto);
        return Result.ok(text2ImageResDto);
    }

    @PostMapping("/list")
    public Result<PageResult<UserResult>> listPage(@RequestBody PageRequestDto pageRequestDto) throws BadRequestException {
        Page<UserResult> page = Page.of(pageRequestDto.getPageNum(),pageRequestDto.getPageSize());
        long userId = - 1;
        if(UserUtils.getUser()!=null){
            userId = UserUtils.getUser().getId();
        }
        page = userResultMapper.selectPage(page, Wrappers.<UserResult>lambdaQuery()
                .eq(UserResult::getUserId, userId)
                .orderByDesc(UserResult::getCreatedTime));
        return Result.ok(PageResult.ok(page.getTotal(),page.getRecords()));
    }

    @PostMapping("/canel")
    public Result<String> canel(@RequestBody @Validated CanelTaskResDto canelTaskResDto) throws BadRequestException {
        return Result.ok(text2ImageService.canel(canelTaskResDto));
    }

    @PostMapping("/proprity")
    public Result<Long> proprity(@RequestBody @Validated CanelTaskResDto canelTaskResDto) throws BadRequestException {
        return Result.ok(text2ImageService.proprity(canelTaskResDto));
    }


}